<?php
if (! defined('IN_SITE')) {
	exit('invalid request');
}

/**
 * 分页器。
 */
class Pager {

	/**
	 * 构造一个分页器对象。
	 * @param int $total		记录总条数
	 * @param int $page_size	每页显示的条数
	 * @param int $cur			当前的页码（首页页码为 1）
	 * @param string url		翻页跳转的网址，跳转时将追加 p=n 参数
	 */
	public function __construct($total, $page_size, $cur, $url)
	{
		$this->total = intval($total) > 0 ? intval($total) : 0;

		$this->page_size = intval($page_size) > 2 ? intval($page_size) : 2;

		// 计算总页数（最小为 0）
		$this->page_count = intval(ceil($this->total / $this->page_size));

		// 当前页码
		$this->cur = (intval($cur) >= 1 && intval($cur) <= $this->page_count) ? intval($cur) : 1;

		// 其它属性
		$this->baseCls = 'pager';

		$this->url = $url;
	}

	public function getHtml()
	{
		$html = array(
			'<div style="clear:both;float:none;height:0px;"></div></div>'
		);

		if ($this->page_count == 0) {
			// 列表为空
			// TODO: 应显示“无内容”提示
			return '';

		} else if ($this->page_count == 1) {
			// 如果总共只有一页，则什么也不显示
			return '';

		} else {
			// 显示“上一页”
			$html[] = $this->createPrevPage();

			// 显示“首页”
			$html[] = $this->createPageNumber(1);

			// 当前页与首页距离超过 4，显示“分隔”
			if ($this->cur > (1 + 4)) {
				$html[] = $this->createSeperator();
			}

			// 当前页附近的几个页码
			for($p = $this->cur - 3; $p <= $this->cur + 3; $p++) {
				if ($p > 1 && $p < $this->page_count) {
					$html[] = $this->createPageNumber($p);
				}
			}

			// 当前页与末页距离超过 4，显示“分隔”
			if ($this->cur < ($this->page_count - 4)) {
				$html[] = $this->createSeperator();
			}

			// 总共不止一页，需显示“末页”
			if ($this->page_count > 1) {
				$html[] = $this->createPageNumber($this->page_count);
			}

			// 显示“下一页”
			$html[] = $this->createNextPage();

			// 显示“共 n 页”
			$html[] = $this->createTotalPages();

			// 总页数超过 6，需显示“跳到……”
			if ($this->page_count > 6) {
				$html[] = $this->createGotoPage();
			}
		}

		$html[] = '<span class="icon icon-next-page" style="float:right;margin-top:-20px;margin-left:10px;"></span>';
		$html[] = '<div class="' . $this->baseCls . '">';
		$html = array_reverse($html);
		return implode('', $html);
	}

	public function createPageNumber($p)
	{
		if ($p == $this->cur) {
			return '<div class="page_num cur_page">' . $p . '</div>';
		}
		return '<a href="' . $this->url . $p . '"><div class="page_num hover-aware">' . $p . '</div></a>';
	}

	public function createPrevPage()
	{
		if ($this->cur <= 1)
			return '';
		$p = $this->cur - 1;
		return '<a href="' . $this->url . $p . '"><div class="prev_page hover-aware"></div></a>';
	}

	public function createNextPage()
	{
		if ($this->cur >= $this->page_count)
			return '';
		$p = $this->cur + 1;
		return '<a href="' . $this->url . $p . '"><div class="page_num next_page hover-aware">下一页</div></a>';
	}

	public function createSeperator()
	{
		return '<div class="page_num">…</div>';
	}

	public function createTotalPages()
	{
		return '<div class="total_pages">共 ' . $this->page_count . ' 页</div>';
		return '';
	}

	public function createGotoPage()
	{
		return '<div class="goto_page"> &nbsp; 到 <input class="goto_page_num" type="text"> 页 <button onclick="var gotoP = document.getElementsByClassName(\'goto_page_num\')[0].value?document.getElementsByClassName(\'goto_page_num\')[0].value:1;window.location.href=\'' . $this->url . '\'+gotoP;" class="hover-aware">确定</button></div>';
		return '';
	}
}